#!/bin/bash
CURRDIR=`pwd`
INFILE=$1
case $INFILE in
   "")
   echo "No file given. Looking for .wma files in $CURRDIR now."
   ls *.wma>/dev/null 2>&1
   Var1=$?
   if [ $Var1 -lt 1 ] ; then
      echo "Found .wma files."
      echo "Removing spaces in 3 seconds (Ctrl C to stop)."
      sleep 3
      for i in *.wma; do mv "$i" `echo $i | tr ' ' '_'`; done
      echo "Converting to lowercase in 3 seconds (Ctrl C to stop)."
      sleep 3
      for i in *.[Ww][Mm][Aa]; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done
      echo "Converting all WMA to WAV in 3 seconds (Ctrl C to stop)."
      sleep 3
      for i in *.wma; do mplayer $i -ao pcm:file=`basename $i .wma`.wav; done
      echo "Converting all WAV to MP3 in 3 seconds (Ctrl C to stop)."
      sleep 3
      for i in *.wav; do lame -h $i `basename $i .wav`.mp3; done
      echo "Removing all WAV files in 3 seconds (Ctrl C to stop)."
                sleep 3
      find . -name '*.wav' -exec rm {} \;
   else
      echo "No .wma files found."
      $0 help   
   fi
   ;;
   help)
   echo "wma2mp3 by ERA Computer Consulting"
   echo
   echo "Usage:"
   echo "To convert a single .wma use - wma2mp3 <filename>"
   echo "The filename must not contain spaces or special characters."
   echo
   echo "Or run in a directory of several .wma files to convert them all."
   echo
   echo "This script needs the following programs to be in your path for"
   echo "it to work:"
   echo
   echo "pwd, ls, mv, tr, mplayer, basename, lame, find"
   echo
   ;;
   -?)
        echo "wma2mp3 by ERA Computer Consulting"
        echo
   echo "Usage:"
   echo "To convert a single .wma use - wma2mp3 <filename>"
   echo "The filename must not contain spaces or special characters."
   echo
   echo "Or run in a directory of several .wma files to convert them all."
        echo
        echo "This script needs the following programs to be in your path for"
        echo "it to work:"
        echo
        echo "pwd, ls, mv, tr, mplayer, basename, lame, find"
        echo
   ;;
   *)
   echo "File is $INFILE"
   ls `basename $INFILE .wma`.wma>/dev/null 2>&1
   Var1=$?
   if [ $Var1 -lt 1 ] ; then
      echo "Beginning conversion in 5 seconds (Ctrl C to stop)."
      sleep 5
      mv "$INFILE" `echo $INFILE | tr '[A-Z]' '[a-z]'`
      mplayer $INFILE -ao pcm:file=`basename $INFILE .wma`.wav
      lame -h `basename $INFILE .wma`.wav `basename $INFILE .wma`.mp3
      rm `basename $INFILE .wma`.wav
   else
      echo "$INFILE does not appear to be a .wma file."
      echo "Stopping."
   fi
   ;;
esac

